<?php 
/**
 * 大话水浒      将领
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordgenerals{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 显示页面
	 */
	function success(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$uin = SessionInfo('uin');
		//物品配置
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		if (empty($id) || empty($type)){
			return View::error('参数错误.');
		}
		//查询配置物品信息
		$data = App_Overlord_Collocate::getGoods();
		if ($type == 'new'){
			if ($id > 5){
				$id = 5;
			}
			$new_id = $id;
			$id -= 1;
			$prop = App_Overlord_GameStart::getNewUser($id);
			if (empty($prop)){
				return View::error('没有此类礼包.');
			}
			if (!empty($prop)){
				foreach ($prop['prop'] as $k => $row){
					$name[] =  $data[$k][$row[0]]['name'];
					$count[] =  $row[1];
				}
			}
			$num = 4 - $id;
			$new_prop = App_Overlord_GameStart::getNewUser($new_id);
			if (empty($new_prop)){
				$introduce = '这个最后一个礼包了.';
			}else{
				$introduce = $new_prop['time'].'分钟后可领取下一礼包.';
			}
			if (empty($num)){
				$temp_name = $introduce;
			}else{
				$temp_name = '还可以领取'.$num.'个礼包！' . $introduce;
			}
			if (!empty($name[4])){
				return App_Overlord_GameStart::success('恭喜君上,获得新手礼包！<br />' . $name[0] .' x'.$count[0].'<br />' . $name[1] .' x'.$count[1].'<br />' . $name[2] .' x'.$count[2].'<br />' . $name[3] .' x'.$count[3]. '<br />' .$name[4] .' x'.$count[4].'<br />'.$temp_name ,array(
					array('name' => '查看背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),
					));
			}elseif (!empty($name[3])){
				return App_Overlord_GameStart::success('恭喜君上,获得新手礼包！<br />' . $name[0] .' x'.$count[0].'<br />' . $name[1] .' x'.$count[1].'<br />' . $name[2] .' x'.$count[2].'<br />' . $name[3] .' x'.$count[3].'<br />'.$temp_name ,array(
					array('name' => '查看背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),
					));
			}elseif (!empty($name[2])){
				return App_Overlord_GameStart::success('恭喜君上,获得新手礼包！<br />' . $name[0] .' x'.$count[0].'<br />' . $name[1] .' x'.$count[1].'<br />' . $name[2] .' x'.$count[2].'<br />'.$temp_name ,array(
					array('name' => '查看背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),
					));
			}else{
				return App_Overlord_GameStart::success('恭喜君上,获得新手礼包！<br />' . $name[0] .' x'.$count[0].'<br />' . $name[1] .' x'.$count[1].'<br />'.$temp_name ,array(
					array('name' => '查看背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),
					));
			}
		}elseif ($type == 'city'){
			$to_uin = isset($_GET['to_uin']) ? trim($_GET['to_uin']) : null;
			if (empty($to_uin)){
				return App_Overlord_GameStart::success('成功收取城池税收,获得'.$id.'黄金',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
			}else{
				$user_info = App_Overlord_Query::oneUser($to_uin);
				if (empty($user_info)){
					return View::error('获取用户资料失败!');
				}
				
				return App_Overlord_GameStart::success('成功收取'.$user_info['Value']['name'].'城池税收,获得'.$id.'黄金',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					array('name' => '返回'.$user_info['Value']['name'].'城池' , 'url' => '/app.php?m=overlordcity&amp;a=to_city&amp;to_uin='.$to_uin.'&amp;sid=' . sessionInfo('skey')),
					));
			}
		}elseif ($type == 'to_city'){
			return App_Overlord_GameStart::success('成功收取已占领城池税收,获得'.$id.'黄金',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'capture'){
			$to_uin = ltrim($id, 'city_');
			
			$user_info = App_Overlord_Query::oneUser($to_uin);
			if (empty($user_info)){
				return View::error('获取用户资料失败!');
			}
			return App_Overlord_GameStart::success('占领【'.$user_info['Value']['name'].'】成功,部队正在收取这里的供奉,记得回城查看
			' ,array(
					array('name' => '继续攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
					array('name' => '返回主城' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					array('name' => '返回'.$user_info['Value']['name'].'城池' , 'url' => '/app.php?m=overlordcity&amp;a=to_city&amp;to_uin='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
		}elseif($type == 'level'){
			
			$key = 'user_' . $uin;
			$list = App_Overlord_Query::oneUser($uin);
			if (empty($list)){
				return View::error('获取君主资料失败.');
			}
			if ($list['Value']['gift_lv'] == 7){
				$lv = $list['Value']['gift_lv'] - 4;
			}elseif ($list['Value']['gift_lv'] == 10 || $list['Value']['gift_lv'] == 13){
				$lv = $list['Value']['gift_lv'] - 3;
			}else{
				$lv = $list['Value']['gift_lv'] - 2;
			}
			$award = App_Overlord_GameStart::getLvAward($lv);
			if (!empty($award['award'])){
				foreach ($award['award'] as $k => $row){
					if ($k == 'gold'){
						$name[] =  '黄金';
						$count[] =  $row;
					}else{
						$name[] =  $data[$k][$row[0]]['name'];
						$count[] =  $row[1];
					}
				}
			}
			
			$gift_lv = $list['Value']['gift_lv'];
			return App_Overlord_GameStart::success('恭喜君上，获得'.$lv.'级升级礼包！<br />' . $name[0] .' x'.$count[0].'<br />' . $name[1] .' x'.$count[1] .'<br />' . $name[2] .' x'.$count[2] .'<br />领取下一礼包，请升至'.$gift_lv.'级。');
		}elseif($type == 'garrison'){
			return App_Overlord_GameStart::success('撤防成功.',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
		}elseif($type == 'recover'){
			return App_Overlord_GameStart::success('恭喜君上，城池收复成功.',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
		}elseif($type == 'abandoned'){
			return App_Overlord_GameStart::success('立锥之地，不要也罢！',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'deploy'){
			return App_Overlord_GameStart::success('调兵成功.',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'defense'){
			return App_Overlord_GameStart::success('驻防成功,有了部队的保护,城池会有更多的收入.',array(
					array('name' => '返回城池' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'collect'){
			//收取材料提示
			$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
			$lan = isset($_GET['lan']) ? trim($_GET['lan']) : '';
			$tao = isset($_GET['tao']) ? trim($_GET['tao']) : '';
			$jing = isset($_GET['jing']) ? trim($_GET['jing']) : '';
			$xi = isset($_GET['xi']) ? trim($_GET['xi']) : '';
			$hu = isset($_GET['hu']) ? trim($_GET['hu']) : '';
			if ($lan < 40){
				$lan_name = '金玉石  x'.$lan;
			}else{
				$lan_name = '黄金 +'.$lan;
			}
			if ($tao < 40){
				$tao_name = '青龙石  x'.$tao;
			}else{
				$tao_name = '黄金 +'.$tao;
			}
			if ($jing < 40){
				$jing_name = '玄武石  x'.$jing;
			}else{
				$jing_name = '黄金 +'.$jing;
			}
			if ($xi < 40){
				$xi_name = '朱雀石  x'.$xi;
			}else{
				$xi_name = '黄金 +'.$xi;
			}
			if ($hu < 40){
				$hu_name = '白虎石  x'.$hu;
			}else{
				$hu_name = '黄金 +'.$hu;
			}
			if ($type_id == 0){
				$type_name = '步兵 +'.$id;
			}elseif ($type_id == 1){
				$type_name = '骑兵 +'.$id;
			}elseif ($type_id == 2){
				$type_name = '弓箭兵 +'.$id;
			}elseif ($type_id == 3){
				$type_name = '特种兵 +'.$id;
			}else{
				return View::error('没有此种类型');
			} 
			return App_Overlord_GameStart::success('恭喜君上，在主城中收取到<br />'.$lan_name.'<br />'.$tao_name.'<br />'.$jing_name.'<br />'.$xi_name.'<br />'.$hu_name.'<br />' .$type_name .'<br />',array(
					array('name' => '返回上一页' , 'url' => '/app.php?m=overlordcity&amp;a=material&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'shop'){
			//购买物品提示
			$top_type = isset($_GET['top_type']) ? trim($_GET['top_type']) : '';
			$article_type = isset($_GET['article_type']) ? trim($_GET['article_type']) : '';
			$num = isset($_GET['num']) ? trim($_GET['num']) : 1;
			if ($id == 'gold'){
				$type_name = '黄金';
			}elseif ($id == 'exploits'){
				$type_name = '功勋';
			}elseif ($id == 'diamond'){
				$type_name = '钻石';
			}else{
				return View::error('商店没有此物品.');
			}
			
			if (empty($data[$article_type][$top_type])){
				return View::error('商店没有此物品.');
			}
			$name = $data[$article_type][$top_type]['name'];
			$count = $data[$article_type][$top_type][$id] * $num;
			if ($article_type != 'weapon' && $article_type != 'armour' && $article_type != 'helm' && $article_type != 'horse'){
				$article_type = 'rest';
			}
			return App_Overlord_GameStart::success('成功购买'. $name .'x'.$num.',花费了 ' . $count . $type_name,array(
						array('name' => '查看我的背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),		
						array('name' => '返回商城' , 'url' => '/app.php?m=overlordcommodity&amp;a=mall&amp;type='.$id.'&amp;article_type='.$article_type.'&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'strengthen'){
			//升级装备
			$article_type = isset($_GET['article_type']) ? trim($_GET['article_type']) : '';
			$rand = isset($_GET['rand']) ? trim($_GET['rand']) : '';
			$staff = isset($_GET['staff']) ? trim($_GET['staff']) : '';
			$equip_id = isset($_GET['equip_id']) ? trim($_GET['equip_id']) : '';
			
			if (empty($data[$article_type][$equip_id])){
				return View::error('该装备不存在.');
			}
			//装备名称
			$name = $data[$article_type][$equip_id]['name'];
			//升级装备必备材料ID
			$public_id = 0; 
			if ($article_type == 'weapon'){
				//升级武器材料ID
				$material_id = 3;
				$article_name = '武器';
			}elseif ($article_type == 'armour'){
				//升级盔甲材料ID
				$material_id = 2;
				$article_name = '盔甲';
			}elseif ($article_type == 'helm'){
				//升级头盔材料ID
				$material_id = 1;
				$article_name = '头盔';
			}elseif ($article_type == 'horse'){
				//升级战马材料ID
				$material_id = 4;
				$article_name = '战马';
			}
			//材料名称
			$public_name = $data['material'][$public_id]['name'];
			$material_name = $data['material'][$material_id]['name'];
			
			if (empty($rand)){
				//提示失败
				//当装备升级等级是7的时候
				if ($staff == 7 || empty($rand)) $staff -= 1;
				//装备升级所需要的信息
				$equip_strengthen = App_Overlord_Collocate::getMaterialLv($staff);
				if (empty($equip_strengthen)){
					return View::error('数据异常');
				}
				return App_Overlord_GameStart::success('神马？'.$article_name.$name.'强化失败？当前级别为lv'.$staff.',已消耗'.$public_name.'x'.$equip_strengthen['upgrade'][0].'、'.$material_name.'x'.$equip_strengthen['upgrade'][1] .'！太坑爹了！',array(
					array('name' => '继续强化' , 'url' => '/app.php?m=overlorduser&amp;a=strengthen_page&amp;article_type='.$article_type.'&amp;id='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
			}else{
				//提示成功
				//装备升级所需要的信息
				$equip_strengthen = App_Overlord_Collocate::getMaterialLv($staff-1);
				if (empty($equip_strengthen)){
					return View::error('数据异常');
				}
				return App_Overlord_GameStart::success('福星高照！'.$article_name.$name.'强化成功,当前级别为lv'.$staff.',已消耗'.$public_name.'x'.$equip_strengthen['upgrade'][0].'、'.$material_name.'x'.$equip_strengthen['upgrade'][1],array(
					array('name' => '继续强化' , 'url' => '/app.php?m=overlorduser&amp;a=strengthen_page&amp;article_type='.$article_type.'&amp;id='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
			}
		}elseif ($type == 'generals_lv'){
			//看将领资料
			$list = App_Overlord_Query::oneUin_Generals($uin);
			if (empty($list) || empty($list['Value'][$id])){
				return View::error('数据异常.');
			}
			$row = $list['Value'][$id];
			return App_Overlord_GameStart::success('将领'.$row['name'].'升至'.$row['lv'].'级,攻击 +'.intval($row['grow_ap']).' 生命 +'.intval($row['grow_hp']).' 防御 +'.intval($row['grow_defense']),array(
					array('name' => '返回将领属性' , 'url' => '/app.php?m=overlordgenerals&amp;a=attribute&amp;id='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'prop'){
			$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
			if (empty($data[$id][$type_id])){
				return View::error('没有该道具.');
			}
			$data_info = $data[$id][$type_id];
			return App_Overlord_GameStart::success('使用道具 '.$data_info['name'].' 成功,'.$data_info['introduce'],array(
					array('name' => '返回我的背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'build'){
			$type_id = isset($_GET['type_id']) ? intval($_GET['type_id']) : 0;
			//查询建筑
			$exploit_info = App_Overlord_Query::oneExploit_Build($uin);
			if (empty($exploit_info['Value'][$type_id])){
				return View::error('你还没有该建筑.');
			}
			//功勋建筑列表
			$build = App_Overlord_Collocate::getExploitMaterial();
			if (empty($build[$type_id])){
				return View::error('数据异常.');
			}
			$name = $build[$type_id]['name'];
			return App_Overlord_GameStart::success('【'.$name.'】建筑修建成功,当前拥有'.$name .'建筑'.$exploit_info['Value'][$type_id]['count'].'个,提高驻防收益'.$build[$type_id]['num'].'%',array(
					array('name' => '继续建设' , 'url' => '/app.php?m=overlorduser&amp;a=exploit_build_page&amp;sid=' . sessionInfo('skey')),
					array('name' => '返回' , 'url' => '/app.php?m=overlorduser&amp;a=exploit_build&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'clickgeneral_accomplish'){
			$odds = isset($_GET['odds']) ? trim($_GET['odds']) : '';
			$skill_rand = isset($_GET['skill_rand']) ? trim($_GET['skill_rand']) : '';
			$skills_type = isset($_GET['skills_type']) ? trim($_GET['skills_type']) : '';
			$skill_lv = isset($_GET['skill_lv']) ? trim($_GET['skill_lv']) : '';
			//看将领资料
			$list = App_Overlord_Query::oneUin_Generals($uin);
			if (empty($list) || empty($list['Value'][$id])){
				return View::error('数据异常.');
			}
			$row = $list['Value'][$id];
			//获取训练类型
			$type = $row['train_type'];
			//加载配置数据
			$data = App_Overlord_Collocate::getTrainExp($row['lv']);
			if (empty($data) || empty($data[$type])){
				return View::error('无可用数据.');
			}
			$skill = App_Overlord_Collocate::getSkill();
			
			if (!empty($odds)){
				if (empty($skill[$skills_type][$skill_rand])){
					return View::error('没有该技能.');
				}
				$name = $skill[$skills_type][$skill_rand][$skill_lv]['name'];
				
				return App_Overlord_GameStart::success('将领'.$row['name'].'训练完成<br />将领'.$row['name'].'经验 +'.$data[$type].'<br />君主经验 +2<br />'.$name.'<br />',array(
						array('name' => '查看'.$row['name'].'技能' , 'url' => '/app.php?m=overlordskills&amp;a=generalsskills&amp;id='.$id.'&amp;to_uin=0&amp;sid=' . sessionInfo('skey')),
						array('name' => '<br />返回训练场' , 'url' => '/app.php?m=overlordskills&amp;a=clickgeneral_page&amp;type='.$type.'&amp;sid=' . sessionInfo('skey')),
						));
			}else{
				return App_Overlord_GameStart::success('将领'.$row['name'].'训练完成<br />将领'.$row['name'].'经验 +'.$data[$type].'<br />君主经验 +2',array(
						array('name' => '返回训练场' , 'url' => '/app.php?m=overlordskills&amp;a=clickgeneral_page&amp;type='.$type.'&amp;sid=' . sessionInfo('skey')),
						));
			}
		}elseif ($type == 'clickgeneral_cancel'){
			//看将领资料
			$list = App_Overlord_Query::oneUin_Generals($uin);
			if (empty($list) || empty($list['Value'][$id])){
				return View::error('数据异常.');
			}
			$row = $list['Value'][$id];
			//获取训练类型
			$type = $row['train_type'];
			return App_Overlord_GameStart::success('将领'.$row['name'].'训练被终止<br />将领'.$row['name'].'经验 +0',array(
					array('name' => '返回训练场' , 'url' => '/app.php?m=overlordskills&amp;a=clickgeneral_page&amp;type='.$type.'&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'dismissal'){
			$generals_info = App_Overlord_Query::oneCollocate('app_overlord_generals', $id);
			if (empty($generals_info)){
				return View::error('将领信息不存在.');
			}
			return App_Overlord_GameStart::success('已解雇将领【'.$generals_info['Value']['name'].'】',array(
					array('name' => '返回上一页' , 'url' => '/app.php?m=overlordgenerals&amp;a=generals&amp;sid=' . sessionInfo('skey')),
					));
		}elseif ($type == 'wipe_off'){
			$rand_type = isset($_GET['rand_type']) ? trim($_GET['rand_type']) : '';
			$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
			$rand_skills = isset($_GET['rand_skills']) ? trim($_GET['rand_skills']) : '';
			$rand_skills_num = isset($_GET['rand_skills_num']) ? trim($_GET['rand_skills_num']) : '';
			$skill = App_Overlord_Collocate::getSkill();
			if ($type_id == 1){
				return App_Overlord_GameStart::success('你的将领除天生技能,其他技能已清空.',array(
					array('name' => '返回将领技能' , 'url' => '/app.php?m=overlordskills&amp;a=generalsskills&amp;id='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
			}elseif ($type_id == 0){
				if (empty($skill[$rand_type][$rand_skills][$rand_skills_num])){
					return View::error('数据异常.');
				}
				$name = $skill[$rand_type][$rand_skills][$rand_skills_num]['name'];
				return App_Overlord_GameStart::success('你的将领成功废除技能 '.$name,array(
					array('name' => '返回将领技能' , 'url' => '/app.php?m=overlordskills&amp;a=generalsskills&amp;id='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
			}else {
				return View::error('数据异常.');
			}
		}elseif ($type == 'generaltrain'){
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			if (empty($generals_info['Value'][$id])){
				return View::error('数据异常!');
			}
			if ($generals_info['Value'][$id]['train_type'] == 2){
				$name = '统御训练';
			}elseif ($generals_info['Value'][$id]['train_type'] == 1){
				$name = '武技训练';
			}elseif ($generals_info['Value'][$id]['train_type'] == 0){
				$name = '日常训练';
			}
			return App_Overlord_GameStart::success('将领'.$generals_info['Value'][$id]['name'].'正在进行'.$name.',消耗黄金1000.<br />',array(
				array('name' => '返回训练场' , 'url' => '/app.php?m=overlordskills&amp;a=clickgeneral_page&amp;type='.$generals_info['Value'][$id]['train_type'].'&amp;sid=' . sessionInfo('skey')),
				));
		}else{
			return View::error('数据异常.');
		}
	}
	/**
	 * 将领页面
	 * Enter description here ...
	 */
	function generals(){
		$state = isset($_GET['state']) && in_array($_GET['state'] , array('idle' , 'garrison')) ? trim($_GET['state']) : 'idle';
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$to_uin = isset($_GET['to_uin']) ? trim($_GET['to_uin']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
		$city = isset($_GET['city']) ? trim($_GET['city']) : '';
		$task = isset($_GET['task']) ? trim($_GET['task']) : '';
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		if (!empty($to_uin) && !empty($type) && !empty($city)){
			if ($type != 'recover'){
				//相同就返回主页
				if ($to_uin == $uin) {
			//		header('Location: ' . SITEURL . '/app.php?m=overlorduser&sid=' . sessionInfo('skey') );
			//		break;
				}
			}
		}
		$key = 'user_' .$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUin_Generals($uin);
		
		loadLib('App_Overlord_Collocate');
		//获取技能列表
		$data = App_Overlord_Collocate::getSkill();
		loadLib('User');
		if (!empty($result['Value'])){
			foreach ($result['Value'] as $k => $row){
				//给将领重新赋值一个ID
				$result['Value'][$k]['id'] = $k;
				$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
				$result['Value'][$k]['upgrade'] = $generals_lv['upgrade'];
				if ($row['exp'] >= $generals_lv['upgrade']){
					//将领升级
					$info = App_Overlord_GameStart::getGeneralsPromotion($result['Value'][$k], $generals_lv);
					$result['Value'][$k] = $info;
					$statelv = 1;
				}
				if (!empty($row['to_uin']) && $state == 'garrison' && $row['state'] == 1){
					$user_key = ltrim($row['to_uin'], 'city_');
					$user_id[] = $user_key;
				}elseif ($row['state'] != 1) {
					$idle[$k] = $result['Value'][$k];
					$generals_idle_array[] = $idle[$k];
				}
				//体力恢复
				if (!empty($row['physical_time'])){
			
				 	if ($row['physical_time'] - $time < 0){
				 		$physical_time = 3600;
						//将领有恢复体力技能
						if (!empty($row['skills']['study'])){
							foreach ($row['skills']['study'] as $j => $skills_row){
								if ($j == 'train_hp'){
									//减短体力恢复时间
									$skills_num = $data['study'][$j][$skills_row]['num'];
									$physical_time -= intval($physical_time * $skills_num/100);
								}
							}
						}
					 	$num = intval(($time - $row['physical_time'])/$physical_time) + 1;
					 	$row['physical'] += $num;
					 	if ($row['physical'] < 10){
					 		$result['Value'][$k]['physical'] += $num;
					 		$end_time = ($time - $row['physical_time'])%$physical_time;
					 		$result['Value'][$k]['physical_time'] = $time + $physical_time - $end_time;
					 	}else{
					 		//满10或超过10点体力,体力都是满
					 		$result['Value'][$k]['physical'] = 10;
					 		//给将领恢复体力时间归0
					 		$result['Value'][$k]['physical_time'] = 0;
					 		//显示将领体力时的赋值
					 	}
					}
				}
				//训练状态
				if ($row['state'] != 1){
					if (!empty($row['train']) && $row['state'] != 3){
						if ($row['train'] - $time <= 0){
							//训练时间
							$result['Value'][$k]['train'] = 0;
							//训练中的状态
							$result['Value'][$k]['state'] = 3;
							//给一个数赋值,有值就修改将领表
							$train = 1;
						}
					}
				}
			}
			if (!empty($user_id)){
				
				$user_info = App_Overlord_Query::allUser($user_id);
				if (!empty($user_info)){
					foreach ($user_info as $k => $row){
						$temp_key = ltrim($row['Key'] , 'user_');
						$user_array_info[$temp_key] = $row;
					}
				}
				
				foreach ($result['Value'] as $k => $row){
					
					if (!empty($user_array_info[$result['Value'][$k]['to_uin']]['Value']['name'])){
						
						$garrison[$k]['username'] = '';
						$garrison[$k] = $result['Value'][$k];
						$garrison[$k]['username'] = $user_array_info[$result['Value'][$k]['to_uin']]['Value']['name'];
						$generals_garrison_array[] = $garrison[$k];
					}
				}
				
			}
			if (!empty($num) || !empty($train) || !empty($statelv)){
				//修改将领
				$code = App_Overlord_Query::alterUin_generals($uin , $result['Value']);
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=generals');
			}
			$count = 0;
			/*******************数据分页******************/
			$page = ($page-1) * $per_page;
			
			if ($state == 'garrison' && !empty($garrison)){
				//将领等级从高到低排序
				if (!empty($generals_garrison_array)){
					$generals_max_lv = App_Overlord_GameStart::getGeneralsArrSort($generals_garrison_array);
					$count = count($generals_max_lv);
					$list = array_slice($generals_max_lv,$page,$per_page);
				}
			}elseif ($state == 'idle' && !empty($idle)){
				
				//将领等级从高到低排序
				if (!empty($generals_idle_array)){
					
					$generals_max_lv = App_Overlord_GameStart::getGeneralsArrSort($generals_idle_array);
					$count = count($generals_max_lv);
					$list = array_slice($generals_max_lv,$page,$per_page);
				}
			}
			if (!empty($list)){
				$smarty->assign('list',$list);
			}

			$smarty->assign('per_page',$per_page);
			$smarty->assign('count',$count);
			$smarty->assign('uin',$uin);
		}
		
		$smarty->assign('state',$state);
		$smarty->assign('to_uin',$to_uin);
		$smarty->assign('type',$type);
		$smarty->assign('type_id',$type_id);
		$smarty->assign('task',$task);
		$smarty->assign('city',$city);
		return $smarty->fetch('app/Overlord/generals.tpl.php');
	}
	/**
	 * 将领属性
	 */
	function attribute(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$to_uin = isset($_GET['to_uin']) ? intval($_GET['to_uin']) : 0; //外界传递的to_uin
		$city = isset($_GET['city']) ? trim($_GET['city']) : ''; 
		$citytype = isset($_GET['type']) ? trim($_GET['type']) : '';
		$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
		$task = isset($_GET['task']) ? trim($_GET['task']) : '';
		$b_uin = isset($_GET['b_uin']) ? trim($_GET['b_uin']) : '';
		$url = isset($_GET['url']) ? trim($_GET['url']) : 'generals';

		if (empty($to_uin)){
			$uin = sessionInfo('uin');
			if (empty($uin)){
				return View::error('获取君主ID失败.');
			}
		}else{
			$uin = $to_uin;
		}
		
		$generals_key = 'generals_'.$uin;
		$key = 'user_' .$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUin_Generals($uin);

		if (!empty($result['Value'][$id])){
			$row = $result['Value'][$id];
			$user_info = App_Overlord_Query::oneUser($uin);
			//查询兵种		
			$type = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
			$count = 0;
			if (!empty($row['make'])){
				$count = array_sum($row['make']);
			}
			
			$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
			
			$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
			//体力恢复
			if (!empty($row['physical_time'])){
				 if ($row['physical_time'] - $time < 0){
					$physical_time = 3600;
					//将领有恢复体力技能
					if (!empty($row['skills']['study'])){
						foreach ($row['skills']['study'] as $j => $skills_row){
							if ($j == 'train_hp'){
								//减短体力恢复时间
								$skills_num = $data['study'][$j][$skills_row]['num'];
								$physical_time -= intval($physical_time * $skills_num/100);
							}
						}
					}
				 	$num = intval(($time - $row['physical_time'])/$physical_time) + 1;
				 	$row['physical'] += $num;
					
				 	if ($row['physical'] < 10){
				 		$result['Value'][$id]['physical'] += $num;
				 		$end_time = ($time - $row['physical_time'])%$physical_time;
				 		$result['Value'][$id]['physical_time'] = $time + $physical_time - $end_time;
				 		$row['physical_time'] = $time + $physical_time - $end_time;
				 		$row['physical_end_time'] = App_Overlord_GameStart::getEndDate($row['physical_time']);
				 	}else{
				 		//满10或超过10点体力,体力都是满
				 		$result['Value'][$id]['physical'] = 10;
				 		//给将领恢复体力时间归0
				 		$result['Value'][$id]['physical_time'] = 0;
				 		//显示将领体力时的赋值
				 		$row['physical'] = 10;
				 	}
				 	//修改将领
					App_Overlord_Query::alterUin_generals($uin , $result['Value']);
				 }else{
				 	$row['physical_end_time'] = App_Overlord_GameStart::getEndDate($row['physical_time']);
				 }
				
			}
			
			//判断是不是自己的UIN
			if ($uin == sessionInfo('uin')){
				if ($row['exp'] >= $generals_lv['upgrade']){
					//将领升级
					$info = App_Overlord_GameStart::getGeneralsPromotion($result['Value'][$id], $generals_lv);
					$result['Value'][$id] = $info;
					
					//修改将领
					App_Overlord_Query::alterUin_Generals($uin  , $result['Value']);
					App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=generals_lv&id='.$id);
				}
			}
		
			//技能列表
			loadLib('App_Overlord_Collocate');   
			$data = App_Overlord_Collocate::getSkill();
		
			$smarty = loadSmarty();
			$smarty->assign('to_uin',$to_uin);
			$smarty->assign('list',$row);
			$smarty->assign('count',$count);
			$smarty->assign('upgrade',$generals_lv['upgrade']);
			$smarty->assign('type',$type);
			$smarty->assign('id',$id);
			$smarty->assign('city',$city);
			$smarty->assign('data',$data);
			
			$smarty->assign('url',$url);
			$smarty->assign('type_id',$type_id);
			$smarty->assign('b_uin',$b_uin);
			$smarty->assign('task',$task);
			$smarty->assign('citytype',$citytype);
			return $smarty->fetch('app/Overlord/attribute.tpl.php');
		}else{
			return View::error('没有该将领信息.');
		}
	}
	/**
	 * 将领装备
	 */
	function equip(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$to_uin = isset($_GET['to_uin']) ? intval($_GET['to_uin']) : 0; //外界传递的to_uin
		$url = isset($_GET['url']) ? trim($_GET['url']) : 'generals';
		if (empty($to_uin)){
			$uin = sessionInfo('uin');
			if (empty($uin)){
				return View::error('获取君主ID失败.');
			}
		}else{
			$uin = $to_uin;
		}
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		$smarty = loadSmarty();
		$smarty->assign('list',$generals_info['Value'][$id]);
	
		$smarty->assign('id',$id);
		$smarty->assign('to_uin',$to_uin);
		$smarty->assign('url',$url);
		return $smarty->fetch('app/Overlord/equip.tpl.php');
	}
	/**
	 * 更换装备
	 * Enter description here ...
	 */
	function renewal(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$url = isset($_GET['url']) ? trim($_GET['url']) : 'generals';
		$smarty = loadSmarty();
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		if($page <= 0){
			$page = 1;
		}
		$count = 0;
		//控制显示数据数量
		$per_page = 10;
		/*******************数据分页******************/
		$offset = ($page-1) * $per_page;
		
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		if (empty($type)){
			return View::error('参数错误.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询将领
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 1){
			return View::error('该将领正在驻防中,无法更换装备.');
		}
		//加载物品配置
		loadLib('App_Overlord_Collocate');
		//查询配置信息
		$data = App_Overlord_Collocate::getGoods();
		//查询背包的内容
		$equipnature = App_Overlord_Collocate::getEquipNature();
		
		$items_info = App_Overlord_Query::oneItems($uin);
		if (!empty($items_info['Value']['equip'][$type])){
			foreach ($items_info['Value']['equip'][$type] as $k => $row){
				$items_info['Value']['equip'][$type][$k]['name'] = $data[$type][$row['id']]['name'];
				$items_info['Value']['equip'][$type][$k]['equip_lv'] = $data[$type][$row['id']]['lv'];
				$equip_lv = $items_info['Value']['equip'][$type][$k]['equip_lv'];
				//获取一级不同装备的数值
				if ($type == 'weapon'){
					//武器
					if ($data[$type][$row['id']]['num'] == 6){
						$equip_lv = $equip_lv . '_1';
					}elseif ($data[$type][$row['id']]['num'] == 12){
						$equip_lv = $equip_lv . '_2';
					}
				}
				if ($type == 'armour'){
					//防具
					if ($data[$type][$row['id']]['num'] == 4){
						$equip_lv = $equip_lv . '_1';
					}elseif ($data[$type][$row['id']]['num'] == 7){
						$equip_lv = $equip_lv . '_2';
					} 
				}
				if ($type == 'helm'){
					//头盔
					if ($data[$type][$row['id']]['num'] == 12){
						$equip_lv = $equip_lv . '_1';
					}elseif ($data[$type][$row['id']]['num'] == 25){
						$equip_lv = $equip_lv . '_2';
					} 
					
				}
				if ($type == 'horse'){
					//战马
					if ($data[$type][$row['id']]['num'] == 40){
						$equip_lv = $equip_lv . '_1';
					}elseif ($data[$type][$row['id']]['num'] == 80){
						$equip_lv = $equip_lv . '_2';
					} 
				}
				if (!empty($equipnature[$type][$equip_lv][$row['lv']])){
					$items_info['Value']['equip'][$type][$k]['equip_ap'] = $equipnature[$type][$equip_lv][$row['lv']];
				}
				if ($row['state'] == 0){
					$count += 1;
				}
			}
			$smarty->assign('list',$items_info['Value']['equip'][$type]);
			$smarty->assign('type',$type);
		}
		$smarty->assign('per_page',$per_page);
		$smarty->assign('offset',$offset);
		$smarty->assign('count',$count);
		$smarty->assign('id',$id);
		$smarty->assign('url' , $url);
		return $smarty->fetch('app/Overlord/renewal.tpl.php');
	}
	/**
	 * 确认换装
	 * Enter description here ...
	 */
	function generalsequip(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0; //将领的ID
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';  //装备的类
		$type_id = isset($_POST['type_id']) ? trim($_POST['type_id']) : ''; //装备的ID
		$url = isset($_POST['url']) ? trim($_POST['url']) : 'generals';
		
		// ================================= post传递值的有效性 =======================
		if (empty($id) || empty($type) || empty($type_id)) {
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询将领
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 1){
			return View::error('该将领正在驻防中,无法更换装备.');
		}
		//加载物品配置
		loadLib('App_Overlord_Collocate');
		//查询配置信息
		$data = App_Overlord_Collocate::getGoods();
		//查询装备的属性配置
		$equipnature = App_Overlord_Collocate::getEquipNature();
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		
		if (empty($items_info['Value']['equip'][$type][$type_id]) || empty($data[$type][$items_info['Value']['equip'][$type][$type_id]['id']])){
			return View::error('没有该装备.');
		}
		if ($type == 'weapon'){
			$generals_type = 'ap';
		}elseif ($type == 'armour'){
			$generals_type = 'defense';
		}elseif ($type == 'helm'){
			$generals_type = 'hp';
		}elseif ($type == 'horse'){
			$generals_type = 'make_count';
		}
		//装备等级大于将领等级时
		if ($data[$type][$items_info['Value']['equip'][$type][$type_id]['id']]['lv'] > $generals_info['Value'][$id]['lv']){
			return View::error('你的将领等级不足.' , 0 ,array(
				array('name' => '去训练' , 'url' => '/app.php?m=overlordskills&amp;a=clickgeneral&amp;id='.$id.'&amp;sid=' . sessionInfo('skey')),
					));
		}
		//状态0代表装备没被其他将领带
		if ($items_info['Value']['equip'][$type][$type_id]['state'] != 0){
			return View::error('你的装备发生了变化.');
		}
		//初始攻击力
		$num = 0;
		//初始技能加成
		$skills_num = 0;
		//装备信息
		$items_equip_info = $items_info['Value']['equip'][$type][$type_id];
		$equip_lv = $data[$type][$items_equip_info['id']]['lv'];
		//获取一级不同装备的数值
		if ($type == 'weapon'){
			//武器
			if ($data[$type][$items_equip_info['id']]['num'] == 6){
				$equip_lv = $equip_lv . '_1';
			}elseif ($data[$type][$items_equip_info['id']]['num'] == 12){
				$equip_lv = $equip_lv . '_2';
			}
		}
		if ($type == 'armour'){
			//防具
			if ($data[$type][$items_equip_info['id']]['num'] == 4){
				$equip_lv = $equip_lv . '_1';
			}elseif ($data[$type][$items_equip_info['id']]['num'] == 7){
				$equip_lv = $equip_lv . '_2';
			} 
		}
		if ($type == 'helm'){
			//头盔
			if ($data[$type][$items_equip_info['id']]['num'] == 12){
				$equip_lv = $equip_lv . '_1';
			}elseif ($data[$type][$items_equip_info['id']]['num'] == 25){
				$equip_lv = $equip_lv . '_2';
			} 
		}
		if ($type == 'horse'){
			//战马
			if ($data[$type][$items_equip_info['id']]['num'] == 40){
				$equip_lv = $equip_lv . '_1';
			}elseif ($data[$type][$items_equip_info['id']]['num'] == 80){
				$equip_lv = $equip_lv . '_2';
			} 
		}
		//装备属性
		$num = $equipnature[$type][$equip_lv][$items_equip_info['lv']];
		
		//将领有这个武器加攻击的技能
		if (!empty($generals_info['Value'][$id]['skills']['study'])){
			//加载技能列表
			loadLib('App_Overlord_Collocate');
			$skills = App_Overlord_Collocate::getSkill();
			//如果有加攻击技能
			foreach ($generals_info['Value'][$id]['skills']['study'] as $k => $row){
				if ($k == $type){
					$skills_num = $skills['study'][$k][$row]['num'];
					$num += $num * $skills_num/100;
					break;
				}
			}
		} 
		
		//将装备的属性加上
		$generals_info['Value'][$id][$generals_type] += $num;
		
		//判断将领已有装备
		if (!empty($generals_info['Value'][$id]['equipment'][$type])){
			//将领身上在包囊装备ID
			$eq_id = $generals_info['Value'][$id]['equipment'][$type]['id'];
			//这个是将领身上的装备
			if (empty($items_info['Value']['equip'][$type][$eq_id])){
				return View::error('数据异常.');
			}
			//将领已带装备ID信息
			$it_id = $items_info['Value']['equip'][$type][$eq_id];
			//将领已带装备等级
			$it_data_lv = $data[$type][$it_id['id']]['lv'];
			//获取一级不同装备的数值
			if ($type == 'weapon'){
				//武器
				if ($data[$type][$it_id['id']]['num'] == 6){
					$it_data_lv = $it_data_lv . '_1';
				}elseif ($data[$type][$it_id['id']]['num'] == 12){
					$it_data_lv = $it_data_lv . '_2';
				} 
			}
			if ($type == 'armour'){
				//防具
				if ($data[$type][$it_id['id']]['num'] == 4){
					$it_data_lv = $it_data_lv . '_1';
				}elseif ($data[$type][$it_id['id']]['num'] == 7){
					$it_data_lv = $it_data_lv . '_2';
				} 
			}
			if ($type == 'helm'){
				//头盔
				if ($data[$type][$it_id['id']]['num'] == 12){
					$it_data_lv = $it_data_lv . '_1';
				}elseif ($data[$type][$it_id['id']]['num'] == 25){
					$it_data_lv = $it_data_lv . '_2';
				} 
			}
			if ($type == 'horse'){
				//战马
				if ($data[$type][$it_id['id']]['num'] == 40){
					$it_data_lv = $it_data_lv . '_1';
				}elseif ($data[$type][$it_id['id']]['num'] == 80){
					$it_data_lv = $it_data_lv . '_2';
				} 
			}
			//装备属性
			$is_num = $equipnature[$type][$it_data_lv][$it_id['lv']];
			//扣除将领原有装备的属性,战马不加
			$generals_info['Value'][$id][$generals_type] -= $is_num;
			
			//原有的装备修改状态
			$items_info['Value']['equip'][$type][$eq_id]['state'] = 0;
		}
		//将装备改成已带状态
		$items_info['Value']['equip'][$type][$type_id]['state'] = 1;
		//给将领带上装备
		$generals_info['Value'][$id]['equipment'][$type] = array('id' => $type_id, 'name' => $data[$type][$items_info['Value']['equip'][$type][$type_id]['id']]['name'], 'lv' => $items_info['Value']['equip'][$type][$type_id]['lv'], 'num' => $num);

		//修改背包里的装备
		App_Overlord_Query::alterItems($uin, $items_info['Value']);							
		
		//修改将领里的状态
		App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
		
		//完成换装任务
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		App_Overlord_Task::getGo('freshman' , $task_type['freshman']['generals_equip']);
		
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=equip&id='.$id .'&url=' .$url);
	}
	/**
	 * 卸装备
	 */
	function unsnatchequip(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0; //将领的ID
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';  //装备的类
	
		// ================================= post传递值的有效性 =======================
		if (empty($id) || empty($type)) {
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询将领
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 1){
			return View::error('该将领正在驻防中,无法卸下装备.');
		}
		if (empty($generals_info['Value'][$id]['equipment'][$type])){
			return View::error('你还没穿该装备.');
		}
		$row = $generals_info['Value'][$id]['equipment'][$type];
		//加载物品配置
		loadLib('App_Overlord_Collocate');
		//查询配置信息
		$data = App_Overlord_Collocate::getGoods();
		//查询装备的属性配置
		$equipnature = App_Overlord_Collocate::getEquipNature();
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		//装备状态修改
		$items_info['Value']['equip'][$type][$row['id']]['state'] = 0;
		if ($type == 'weapon'){
			$generals_type = 'ap';
		}elseif ($type == 'armour'){
			$generals_type = 'defense';
		}elseif ($type == 'helm'){
			$generals_type = 'hp';
		}elseif ($type == 'horse'){
			$generals_type = 'make_count';
		}
	
		//将领属性
		if (empty($generals_info['Value'][$id]['equipment'][$type]['num'])){
			$generals_info['Value'][$id]['equipment'][$type]['num'] = 0;
		}
		//减属性
		$generals_info['Value'][$id][$generals_type] -= $generals_info['Value'][$id]['equipment'][$type]['num'];
		$value = array();
		foreach ($generals_info['Value'][$id]['equipment'] as $k => $row){
			if ($type != $k){
				$value[$k] = $row;
			}
		}
		
		//将领装备重新赋值
		$generals_info['Value'][$id]['equipment'] = $value;
		//背包修改
		App_Overlord_Query::alterItems($uin, $items_info['Value']);
		//将领修改
		App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=equip&id='.$id);
	}
	/**
	 * 确认战斗策略
	 */
	function strategy(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0; //将领的ID
		$url = isset($_GET['url']) ? trim($_GET['url']) : ''; //将领的ID
		
		// ================================= post传递值的有效性 =======================
		if (empty($id)) {
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询将领
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 1){
			return View::error('该将领正在驻防中,无法更改战斗策略.');
		}
	
		$smarty = loadSmarty();
		$smarty->assign('list',$generals_info['Value'][$id]);
		$smarty->assign('id',$id);
		$smarty->assign('url' , $url);
		return $smarty->fetch('app/Overlord/strategy.tpl.php');
	}
	/**
	 * 策略介绍
	 * Enter description here ...
	 */
	function strategy_page() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0; //将领的ID
		$type = isset($_GET['type']) ? intval($_GET['type']) : 0; //将领的ID
		// ================================= post传递值的有效性 =======================
		if (empty($id)) {
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询将领
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 1){
			return View::error('该将领正在驻防中,无法更改战斗策略.');
		}
		$smarty = loadSmarty();
		$smarty->assign('list',$generals_info['Value'][$id]);
		$smarty->assign('id',$id);
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/strategy_page.tpl.php');
	}
	/**
	 * 确认修改战斗策略
	 * Enter description here ...
	 */
	function strategy_execute(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : 0; //将领的ID
		$url = isset($_POST['url']) ? trim($_POST['url']) : 0; //将领的url
		$strategy = isset($_POST['strategy']) ? intval($_POST['strategy']) : 0; //将领的ID
		
		// ================================= post传递值的有效性 =======================
		if (empty($id)) {
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询将领
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有将领.');
		}
		if (empty($generals_info['Value'][$id])){
			return View::error('没有该将领信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 1){
			return View::error('该将领正在驻防中,无法更改战斗策略.');
		}
		//修改战略
		$generals_info['Value'][$id]['strategy'] = $strategy;
		//修改将领资料
		App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
		
		//完成修改战略任务
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		App_Overlord_Task::getGo('main' , $task_type['main']['tactics']);
		
		//跳转页面
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=strategy&url='.$url.'&id='.$id);
	}
	
	/**
	 * 玩法说明
	 * Enter description here ...
	 */
	function explain(){
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$smarty = loadSmarty();
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/explain.tpl.php');
	}

	/**
	 * 解雇将领确认页面
	 */
	function dismissal_page(){
		$id = isset($_GET['generals']) ? trim($_GET['generals']) : '';
		if (empty($id)){
			return View::error('参数错误.');
		}
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUin_Generals($uin);
		
		if (!empty($result['Value'][$id])){
			$generals_info = $result['Value'][$id];
			if ($generals_info['state'] != 0){
				return View::error('无法解雇.');
			}
			$smarty = loadSmarty();
			$smarty->assign('list',$generals_info);
			$smarty->assign('id',$id);
			return $smarty->fetch('app/Overlord/dismissal_page.tpl.php');
		}else{
			return View::error('数据异常.');
		}
	}
	/**
	 * 解雇将领
	 * Enter description here ...
	 */
	function dismissal(){
		$id = isset($_POST['generals']) ? trim($_POST['generals']) : '';
		if (empty($id)){
			return View::error('参数错误.');
		}
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUin_Generals($uin);
		
		if (!empty($result['Value'][$id])){
			$generals_info = $result['Value'][$id];
			if ($generals_info['state'] != 0){
				return View::error('无法解雇.');
			}
			if (!empty($generals_info['equipment'])){
				$items_info = App_Overlord_Query::oneItems($uin);
				foreach ($generals_info['equipment'] as $k => $row){
					//卸下装备
					$items_info['Value']['equip'][$k][$row['id']]['state'] = 0;
				}
				//重新更新背包
				App_Overlord_Query::alterItems($uin, $items_info['Value']);
			}
	
			//找出将领
			foreach ($result['Value'] as $j => $rs){
	
				if ($id != $j){
			
					$list['Value'][$j] = $rs;
				}else{
					print_r($rs);
					$id = $rs['generals'];
				}
			}
			//重新更新将领
			App_Overlord_Query::alterUin_Generals($uin, $list['Value']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=dismissal&id='.$id);
		}else{
			return View::error('数据异常.');
		}
	}
}
# end of script